package com.example.flowableoa.controller;


import com.example.comment_oa.pojos_vo.flowableVo.taskVo;
import com.example.comment_oa.response.AjaxResponse;
import com.example.flowableoa.domain.dto.FlowTaskDto;
import com.example.flowableoa.service.IFlowTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("flowableOa")
public class FlowTaskController {

    @Autowired
    private IFlowTaskService flowTaskService;

    /*\
    *
    *   查询我的代办的任务
     */
    @GetMapping("/mywait/{GongHao}")
    public AjaxResponse myToDoTask(@PathVariable("GongHao") String GongHao){
        List<FlowTaskDto> flowTaskDtos = flowTaskService.myTodoTaskList(GongHao);
//        System.err.println(flowTaskDtos);
//        System.err.println(flowTaskDtos.get(0).getVariableInstances());
        return  AjaxResponse.success(flowTaskDtos);
    }


    /*
    /  查看我发起的 任务
     */
    @GetMapping("/mysend/{GongHao}")
    public AjaxResponse myfaqided(@PathVariable("GongHao") String GongHao){
        List<FlowTaskDto> flowTaskDtos = flowTaskService.mysendTaskList(GongHao);

        return  AjaxResponse.success(flowTaskDtos);
    }



//    @PostMapping(value = "/flowImg/{deployId}")
////    @ResponseBody
//    public AjaxResponse flowImg(@PathVariable("deployId") String deployId){
//        System.err.println("图片的ID："+deployId);
//        byte[] bytes = iflowDefinitionService.flowImg(deployId);
//        System.err.println("字节码："+bytes.length);
//        System.err.println("字节码："+bytes);
//        return new AjaxResponse(bytes);
//    }
//    procInsId
    /*
    查看过程流程图
     */
    @PostMapping(value = "/flowImg2/{procInsId}")
    @ResponseBody
    public AjaxResponse flowImg(@PathVariable("procInsId") String procInsId) throws Exception{
        //System.out.println("进来了么");
        //System.err.println("图片的ID："+procInsId);
        InputStream diagram = flowTaskService.diagram(procInsId);
        byte[] bytes = new byte[diagram.available()];
        diagram.read(bytes);
        //System.err.println("字节码："+bytes);
        return new AjaxResponse(bytes);
    }

    /*
    完成任务 --拒绝
     */
    @PostMapping("/wanchengrenwu")
    public AjaxResponse wancehnt(@RequestBody taskVo taskVO){
        flowTaskService.complete(taskVO);
        return AjaxResponse.success();
    }

    /*
   完成任务 --同意
    */
    @PostMapping("/wanchengrenwu2")
    public AjaxResponse wancehnttongyi(@RequestBody taskVo taskVO){
        flowTaskService.completetongyi(taskVO);
        return AjaxResponse.success();
    }

    /**
     * 我的已办
     *    查询我发起的已经完成的任务
     * @return
     */
    @GetMapping("/finishedList/{GongHao}")
    public AjaxResponse getFinishedList(@PathVariable("GongHao") String GongHao){
        List<FlowTaskDto> flowTaskDtos = flowTaskService.finishedList(GongHao);
        return  AjaxResponse.success(flowTaskDtos);
    }


}
